package cn.limbo.myandserver.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import cn.limbo.myandserver.model.FileModel;
import cn.limbo.myandserver.model.User;

import java.util.ArrayList;

/**
 * 数据库工具管理类
 */
public class DatabaseUtils {

    /**
     * 数据库辅助类对象
     */
    private SQLiteOpenHelper dbHelper;

    /**
     * 构造方法，创建一个数据库辅助类对象
     * @param context 上下文对象
     */
    public DatabaseUtils(Context context) {
        dbHelper = new DatabaseHelper(context);
    }

    // 增。向数据库中增加一组数据
    public long add(String account, String password) {
        SQLiteDatabase database = dbHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(User.ACCOUNT, account);
        values.put(User.PASSWORD, password);
        long insert = database.insert(User.TABLE_NAME, null, values);
        database.close();
        return insert;
    }

    // 查。查询数据库中的账户信息，返回一个用户信息列表。
    public ArrayList<User> query() {
        ArrayList<User> list = new ArrayList<>();

        SQLiteDatabase database = dbHelper.getReadableDatabase();
        String select = "SELECT " +
                User.ACCOUNT + ", " +
                User.PASSWORD +
                " FROM " + User.TABLE_NAME;
        Cursor cursor = database.rawQuery(select, null);

        cursor.moveToFirst();
        while (!cursor.isAfterLast()) {
            String account = cursor.getString(0);
            String password = cursor.getString(1);
            list.add(new User(account, password));
            cursor.moveToNext();
        }
        cursor.close();
        database.close();
        return list;
    }

    // 增。向文件数据库中增加一组数据
    public long addToFile(String fileName, String fileMD5) {
        SQLiteDatabase database = dbHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(FileModel.FILE_NAME, fileName);
        values.put(FileModel.FILE_MD5, fileMD5);
        long insert = database.insert(FileModel.FILE_TABLE_NAME, null, values);
        database.close();
        return insert;
    }

    // 查。查询文件数据库中的账户信息，返回一个文件信息列表。
    public ArrayList<FileModel> queryFromFile() {
        ArrayList<FileModel> list = new ArrayList<>();

        SQLiteDatabase database = dbHelper.getReadableDatabase();
        String select = "SELECT " +
                FileModel.FILE_NAME + ", " +
                FileModel.FILE_MD5 +
                " FROM " + FileModel.FILE_TABLE_NAME;
        Cursor cursor = database.rawQuery(select, null);

        cursor.moveToFirst();
        while (!cursor.isAfterLast()) {
            String fileName = cursor.getString(0);
            String fileMD5 = cursor.getString(1);
            list.add(new FileModel(fileName, fileMD5));
            cursor.moveToNext();
        }
        cursor.close();
        database.close();
        return list;
    }

    //查。通过文件名查找对应的MD5值。
    public String queryByFileName(String fileName) {
        SQLiteDatabase database = dbHelper.getReadableDatabase();
        String select = "SELECT " +
                FileModel.FILE_MD5 +
                " FROM " + FileModel.FILE_TABLE_NAME +
                " WHERE " + FileModel.FILE_NAME + " = " + fileName;
        Cursor cursor = database.rawQuery(select, null);
        cursor.moveToFirst();
        String fileMD5 = cursor.getString(0);
        cursor.close();
        database.close();
        return fileMD5;
    }
}
